var ctx="";
var swiperCar=null;
var virtual=0;
var mustMemberInfo=false;

$(document).ready(function(){ 
	
//	app.loadData();
	$.post("/driver/api/rest/v3/completeTask",{
		"driverId":329,
		"orderId":2153,
		"shouldCash":39.0,
		"mileage":0.0,
		"mileageMoney":0.0,
		"waitedTime":0,
		"waitedMoney":0.0,
		"waitStartTime":1419575806542,
		"travelBeginTime":1419646107236,
		"appKey":"fd7728895d464dc89b0735cb3d565ce2",
		"token":"ccc",
		"timestamp":1420443122923
	},function(result){
		init(result);
	});
	
	$("#submit_botton").click(function(){
		
		if($.trim($("#realCash").val())==''){
			$("#Info").html("请填写实收金额");
    		$('#dialog_Info').modal();
    		return false;
		}
		
		if($("input[name=payType]:checked").length==0){
			$("#Info").html("请选择支付方式");
    		$('#dialog_Info').modal();
    		return false;
    	}
		
		if(mustMemberInfo){
			if($.trim($("#carNumber").val())==''){
				$("#Info").html("请填写客户信息");
	    		$('#dialog_Info').modal();
	    		return false;
			}
		}
		
		app.load();
		$.post(ctx+"/driver/api/rest/v3/completeTask2",$('#submit_form').serialize(),function(data){
	        if(data.code=="0"){

	    		app.back();

	        } else {
	        	$("#Info").html(data.message);
    			$('#dialog_Info').modal();
    			app.cancelLoad();
	        }
		},'json');
    	
	});
	
	
	//选择支付类型
	$('input[name=payType]').change(function(){
	 	showMoneyDetails();
	});
	
	//变更实收金额
	$('#realCash').change(function(){
		var realCash=$(this).val();
		$(this).val(parseFloat(realCash).toFixed(1));
	 	showMoneyDetails();
	});
	
	//下拉优惠劵
	$(document).on("click",".slidedown_coupon",function(){
		$(this).find(".pull-right img").attr("src",ctx+"/assets/css/images/coupons_icon2@2x.png");
		$("#coupon_li").slideDown();
		$(this).removeClass("slidedown_coupon").addClass("slideup_coupon");
	});
	$(document).on("click",".slideup_coupon",function(){
		$(this).find("img").attr("src",ctx+"/assets/css/images/coupons_icon1@2x.png");
		$("#coupon_li").slideUp();
		$(this).removeClass("slideup_coupon").addClass("slidedown_coupon");
	});
	
	//选择优惠劵
	$(document).on("click",".slide_box",function(){
		if($(this).hasClass("coupon-chosed")){
			$(".slide_box").removeClass("coupon-chosed");
			$(".coupon-xz img").remove();
		}
		else{
			$(".slide_box").removeClass("coupon-chosed");
			$(".coupon-xz img").remove();
			$(this).addClass("coupon-chosed");
			$(this).find(".coupon-xz").html('<img src="'+ctx+'/assets/css/images/yh_xc@2x.png" width="40" height="28">');
		}
		showMoneyDetails();
	});
	
});

function init(result){
	
//	var resultObj = eval('(' + result + ')');
	var resultObj = result;
	
	var code=resultObj.code;
	if(code==0){
		var data=resultObj.data;
		
		ctx=data.ctx;
		virtual=parseFloat(data.virtual);
		mustMemberInfo=data.mustMemberInfo;
		
		//formdata
		$("#driverId").val(data.driverId);
		$("#orderId").val(data.orderId);
		$("#shouldCash").val(data.shouldCash);
		$("#mileage").val(data.mileage);
		$("#waitedTime").val(data.waitedTime);
		$("#waitStartTime").val(data.waitStartTime);
		$("#travelBeginTime").val(data.travelBeginTime);
		$("#discount").val(data.discount);
		
		//1
		$("#memberPhone_b").html(data.memberPhone);
		$("#memberName_span").html(data.memberName);
		$("#memberType_b").html(data.memberType);
		$("#carNumber").val(data.memberCarNumber);
		
		if(data.memberType=="企业用户"){
			$("#memberCompanyName_span").html("("+data.memberCompanyName+")");
		}
		if(data.isOverdraw){
			$("#isOverdraw_i").html("(可签单)");
		}
		
		if(null!=data.memberEcp){
			$("#memberEcp_b").html(data.memberEcp);
			$("#memberEcp_a").attr("href","tel:"+data.memberEcp);
			$("#memberEcn_span").html("("+data.memberEcn+")");
			$("#memberEc_li").show();
		}
		
		$("#virtual_b").text(data.virtual+"元");
		$("#couponMoney_b").text(data.couponMoney+"元");
		
		//2
		$("#orderNumber_b").text(data.orderNumber);
		$("#orderType_span").text(data.orderType);
		$("#orderCompanyName_span").text(data.orderCompanyName);
		$("#startDriveDate_span").text(data.startDriveDate);
		$("#qibuMoney_span").text(data.qibuMoney);
		$("#waitedTime_span").text(data.waitedTime);
		$("#waitedMoney_span").text(data.waitedMoney);
		$("#mileage_span").text(data.mileage);
		$("#mileageMoney_span").text(data.mileageMoney);
		$("#shouldCash_span").text(data.shouldCash);
		
		if(data.discount!=null){
			$("#discount_i").text(data.discount+"折");
			$("#discount_i").show();
			$("#realCash").val((data.shouldCash*data.discount/10).toFixed(1));
		}
		else{
			$("#realCash").val(data.shouldCash);
		}
		
		if(data.virtual<=0){
			$("#payType2").attr("disabled",true);
		}
		if(!data.isOverdraw){
			$("#payType3").attr("disabled",true);
		}
		
		if(null!=data.coupons && data.coupons.length>0){
			$("#couponsAmount_span").text(data.coupons.length);
			$("#couponsImg_span").html('<img src="'+data.ctx+'/assets/css/images/coupons_icon2@2x.png" width="20" height="20" />');
			
			$(data.coupons).each(function(index,coupon){
				$("#coupons_div").append(
				'<div class="swiper-slide">'+
					'<div data-id="'+coupon.id+'" data-money="'+coupon.couponRule.money+'" class="slide_box">'+
			      	    '<div class="coupon-xz"></div>'+
			      		'<div class="coupon-name"><b>'+coupon.couponRule.money+'</b>元优惠券</div>'+
			      		'<div class="coupon-time">结束时间:'+coupon.couponRule.end+'</div>'+
			      		'<div class="coupon-number">'+coupon.code+'</div>'+
			      	'</div>'+
			    '</div>');
			});
			swiperCar = $('.swiper-car').swiper({
				slidesPerSlide : 1
			});
		}
		else{
			$("#coupon_li").remove();
		}
		
		app.closeOrderDialog();//
	}
	else{
		$("#Info").html(resultObj.message);
		$('#dialog_Info').modal();
	}
	
}

function showMoneyDetails(){
	clearFormData();
	var payType=$('input[name=payType]:checked').val();
	if(payType=="cash"){//现金
		var realCash=parseFloat($("#realCash").val());
		if($(".coupon-chosed").length>0){//选择了优惠劵
			var coupnMoney=parseFloat($(".coupon-chosed").attr("data-money"));
			if(coupnMoney>=realCash){
				$("#money_details").html("您已选择：优惠劵"+coupnMoney+"元+现金0元");
				
				$("#couponId").val($(".coupon-chosed").attr("data-id"));
				$("#couponMoney").val(coupnMoney);
				$("#payTypeDetails").val("coupon_cash");
				
			}
			else{
				$("#money_details").html("您已选择：优惠劵"+coupnMoney+"元+现金"+(realCash-coupnMoney).toFixed(1)+"元");
				
				$("#couponId").val($(".coupon-chosed").attr("data-id"));
				$("#couponMoney").val(coupnMoney);
				$("#cashMoney").val((realCash-coupnMoney).toFixed(1));
				$("#payTypeDetails").val("coupon_cash");
			}
		}
		else{
			$("#money_details").html("您已选择：现金"+realCash+"元");
			
			$("#cashMoney").val(realCash);
			$("#payTypeDetails").val("cash");
		}
	}
	else if(payType=="sign"){//签单
		var realCash=parseFloat($("#realCash").val());
		if($(".coupon-chosed").length>0){//选择了优惠劵
			var coupnMoney=parseFloat($(".coupon-chosed").attr("data-money"));
			if(coupnMoney>=realCash){
				$("#money_details").html("您已选择：优惠劵"+coupnMoney+"元+签单0元");
				
				$("#couponId").val($(".coupon-chosed").attr("data-id"));
				$("#couponMoney").val(coupnMoney);
				$("#payTypeDetails").val("coupon_sign");
			}
			else{
				$("#money_details").html("您已选择：优惠劵"+coupnMoney+"元+签单"+(realCash-coupnMoney).toFixed(1)+"元");
			
				$("#couponId").val($(".coupon-chosed").attr("data-id"));
				$("#couponMoney").val(coupnMoney);
				$("#signMoney").val((realCash-coupnMoney).toFixed(1));
				$("#payTypeDetails").val("coupon_sign");
			}
			
		}
		else{
			$("#money_details").html("您已选择：签单"+realCash+"元");
			
			$("#signMoney").val(realCash);
			$("#payTypeDetails").val("sign");
		}
	}
	else if(payType=="balance"){//余额
		var realCash=parseFloat($("#realCash").val());
		
		if(realCash>virtual){//余额不够
			if($(".coupon-chosed").length>0){//选择了优惠劵
				var coupnMoney=parseFloat($(".coupon-chosed").attr("data-money"));
				if(coupnMoney>=realCash){
					$("#money_details").html("您已选择：优惠劵"+coupnMoney+"元+余额0元");
				
					$("#couponId").val($(".coupon-chosed").attr("data-id"));
					$("#couponMoney").val(coupnMoney);
					$("#payTypeDetails").val("coupon_balance");
				}
				else{
					$("#money_details").html("您已选择：优惠劵"+coupnMoney+"元+余额"+(realCash-coupnMoney).toFixed(1)+"元");
				
					$("#couponId").val($(".coupon-chosed").attr("data-id"));
					$("#couponMoney").val(coupnMoney);
					$("#balanceMoney").val((realCash-coupnMoney).toFixed(1));
					$("#payTypeDetails").val("coupon_balance");
				}
			}
			else{
				$("#money_details").html("您已选择：余额"+virtual+"元+现金"+(realCash-virtual).toFixed(1)+"元");
				
				$("#balanceMoney").val(virtual);
				$("#cashMoney").val((realCash-virtual).toFixed(1));
				$("#payTypeDetails").val("balance_cash");
			}
		}
		else{
			if($(".coupon-chosed").length>0){//选择了优惠劵
				var coupnMoney=parseFloat($(".coupon-chosed").attr("data-money"));
				if(coupnMoney>=realCash){
					$("#money_details").html("您已选择：优惠劵"+coupnMoney+"元+余额0元");
				
					$("#couponId").val($(".coupon-chosed").attr("data-id"));
					$("#couponMoney").val(coupnMoney);
					$("#payTypeDetails").val("coupon_balance");
				}
				else{
					$("#money_details").html("您已选择：优惠劵"+coupnMoney+"元+余额"+(realCash-coupnMoney).toFixed(1)+"元");
				
					$("#couponId").val($(".coupon-chosed").attr("data-id"));
					$("#couponMoney").val(coupnMoney);
					$("#balanceMoney").val((realCash-coupnMoney).toFixed(1));
					$("#payTypeDetails").val("coupon_balance");
				}
			}
			else{
				$("#money_details").html("您已选择：余额"+realCash+"元");
				
				$("#balanceMoney").val(realCash);
				$("#payTypeDetails").val("balance");
			}
		}
	}
	else{
		$("#money_details").html("");
		$("#payTypeDetails").val("");
	}
}

function clearFormData(){
	$("#payTypeDetails").val("");
	$("#couponId").val("");
	$("#couponMoney").val(0);
	$("#cashMoney").val(0);
	$("#signMoney").val(0);
	$("#balanceMoney").val(0);
}
